package com.java.cloud.order.controller;

import com.java.cloud.order.entity.Order;
import com.java.cloud.order.feign.PaymentFeign;
import com.java.cloud.order.service.OrderService;
import com.java.cloud.pay.api.PaymentApi;
import com.java.cloud.pay.entity.Payment;
import com.java.common.utils.PageUtils;
import com.java.common.utils.R;
import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Map;

/**
 * @author jiangli
 * @since 2020/3/10 10:39
 */
@RestController
@RequestMapping("/order")
@DefaultProperties(defaultFallback = "globalFallback")
public class OrderController {
	@Autowired
	private PaymentFeign paymentFeign;

	@GetMapping("/create/payment")
	public R createPayment(Payment payment) {
		return paymentFeign.save(payment);
	}

	@GetMapping("/info/payment/{id}")
	//该方法专用的fallback
/*	@HystrixCommand(fallbackMethod = "getPaymentByPaymentIdFallback",commandProperties = {
			@HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds",value = "5000")
	})*/
	//通用fallback
//	@HystrixCommand
	public R<Payment> getPaymentByPaymentId(@PathVariable("id") Long id) {

		return paymentFeign.info(id);
	}

	public R<Payment> getPaymentByPaymentIdFallback(@PathVariable("id") Long id) {
		return R.error("查询失败,请稍后重试!");
	}

	public R<Payment> globalFallback() {
		return R.error("服务器繁忙,请稍后重试!");
	}


	@Autowired
	private OrderService orderService;

	/**
	 * 分页查询order
	 * 当前页码page、每页显示记录数limit
	 */
	@GetMapping("/list")
	public R list(@RequestParam Map<String, Object> params){
		PageUtils page = orderService.queryPage(params);

		return R.ok(page);
	}


	/**
	 * 根据主键查询order
	 */
	@GetMapping("/info/{int}")
	public R info(@PathVariable("id") Long id){
		Order order = orderService.getById(id);

		return R.ok(order);
	}

	/**
	 * 新增order
	 */
	@PostMapping("/save")
	public R save(@RequestBody Order order){
		orderService.create(order);

		return R.ok();
	}

	/**
	 * 修改order
	 */
	@PutMapping("/update")
	public R update(@RequestBody Order order){
		orderService.updateById(order);

		return R.ok();
	}

	/**
	 * 删除order
	 */
	@DeleteMapping("/delete")
	public R delete(@RequestBody Long[] ints){
		orderService.removeByIds(Arrays.asList(ints));

		return R.ok();
	}
}
